﻿using System;
using System.Windows.Forms;
using System.Drawing;

namespace MaterialEditor
{
    public class PlaybackPannel : Control
    {
        /// <summary>
        /// The percentage of the full time the cursor is currently sitting on
        /// </summary>
        private float cursorPoint;
        public float CursorPoint
        {
            get { return cursorPoint; }

            set 
            {
                // wrap value
                if (value > 1.0f)
                    value = 0.0f;
                if (value < 0.0f)
                    value = 1.0f;

                cursorPoint = value;
                
                Invalidate();

                if (CursorPointMoved != null)
                    CursorPointMoved();
            }
        }

        /// <summary>
        /// The maximum amount of time the animation lasts for
        /// </summary>
        public TimeSpan MaxTime = TimeSpan.FromSeconds(10);

        /// <summary>
        /// Gets what time the cursor is currently sitting at
        /// </summary>
        public TimeSpan currentTime
        {
            get
            {
                long max_ticks = MaxTime.Ticks;
                long current_ticks = (long)(max_ticks * cursorPoint);

                return TimeSpan.FromTicks(current_ticks);
            }

            set
            {
                long max_ticks = MaxTime.Ticks;
                long ticks = value.Ticks;

                // focus the control if a second has elapsed
                if (value.Milliseconds == 0)
                    Focus();

                CursorPoint = ticks / (float)max_ticks;
            }
        }

        /// <summary>
        /// The top point cursor
        /// </summary>
        private Point CursorPointLocation
        {
            get
            {
                int x = (int)(Width * cursorPoint);
                return new Point(x, 0);
            }
        }

        /// <summary>
        /// Occours when the cursor point moves left or right
        /// </summary>
        public event BasicDelegate CursorPointMoved;

        /// <summary>
        /// Creates a new instance of playback pannel
        /// </summary>
        public PlaybackPannel()
        {
            // subscribe to events
            MouseMove += PlaybackPannel_MouseMove;
            Paint += PlaybackPannel_Paint;
        }

        void PlaybackPannel_Paint(object sender, PaintEventArgs e)
        {
            System.Drawing.Graphics g = e.Graphics;

            g.DrawLine(Pens.Black, CursorPointLocation, new Point(CursorPointLocation.X, Height));
            Rectangle border = new Rectangle(0, 0, Width, Height);
            border.Inflate(-1, -1);
            g.DrawRectangle(Pens.Black, border);
        }

        void PlaybackPannel_MouseMove(object sender, MouseEventArgs e)
        {
            // move the cursor if the left mouse button is down
            if (e.Button == System.Windows.Forms.MouseButtons.Left)
            {
                CursorPoint = e.X / (float)Width;
            }
        }
    }
}